package org.qloudgen.borg.metadb;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.ibatis.session.SqlSession;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.AppBean;
import org.qloudgen.borg.beans.CacheBean;
import org.qloudgen.borg.beans.Cluster;
import org.qloudgen.borg.beans.DataSourceBean;
import org.qloudgen.borg.beans.Dict;
import org.qloudgen.borg.beans.FuncBean;
import org.qloudgen.borg.beans.KeyValue;
import org.qloudgen.borg.beans.MenuNode;
import org.qloudgen.borg.beans.ParamBean;
import org.qloudgen.borg.beans.RelBean;
import org.qloudgen.borg.beans.ResultBean;
import org.qloudgen.borg.beans.TaskBean;
import org.qloudgen.borg.beans.TaskListBean;
import org.qloudgen.borg.beans.TaskLogBean;
import org.qloudgen.borg.beans.UserBean;
import org.qloudgen.borg.beans.UserRightBean;
import org.qloudgen.borg.messaging.message.TaskMessage;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;


public class DefaultImpl {
	
	public final static List<Dict> getDicts(){
		
		SqlSession sess = CommonValues.factory.openSession();
		List<Dict> result = sess.selectList(ConfigNames.SELECT_DICTS);
		sess.close();
		return result;
				
	}

	public static List<MenuNode> getMenunodes(UserBean user) {
		SqlSession sess = CommonValues.factory.openSession();
		List<MenuNode> result = sess.selectList(ConfigNames.SELECT_MENUNODES,user);
		sess.close();
		return result;
	}
	
	public final static int getClusterID() {

		SqlSession sess = CommonValues.factory.openSession();
		int clusterID = (Integer)sess.selectOne(ConfigNames.SELECT_CLUSTERID);
		sess.close();
		return clusterID;
	}

	public final static void createCluster(Cluster cluster){
		
		if (cluster == null) return;
		
		SqlSession sess = CommonValues.factory.openSession(false);
			
		// insert cluster info 
		sess.insert(ConfigNames.INSERT_CLUSTER,cluster);
			
		sess.commit();
		sess.close();
	}
	

	public final static void getClusterList(List<Cluster> clusters){
		
		SqlSession sess = CommonValues.factory.openSession();
		if (clusters != null)
			clusters.clear();
		else
			clusters = Lists.newArrayList();
		
		List<Cluster> result = sess.selectList(ConfigNames.SELECT_CLUSTER_LIST);
		
		sess.close();
		clusters.addAll(result);
				
	}

	public final static void getAppList(List<AppBean> apps) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (apps != null)
			apps.clear();
		else
			apps = Lists.newArrayList();
		
		List<AppBean> result = sess.selectList(ConfigNames.SELECT_APP_LIST);
		
		KeyValue[] appClusters = sess.selectList(ConfigNames.SELECT_APP_CLUSTER).toArray(new KeyValue[0]);
		
		Map<String,String> map = Maps.newHashMap();

		if (appClusters.length != 0 ){
			
			String appNo = appClusters[0].getKey(), clusterStr = appClusters[0].getValue();
		
			for( int i = 1; i < appClusters.length; i++){
			
				if (appClusters[i].getKey().equals(appNo)){
					clusterStr += ","+appClusters[i].getValue();
				} else {
					map.put(appNo, clusterStr);
					appNo = appClusters[i].getKey();
					clusterStr = appClusters[i].getValue();
				}
				
			}	
			map.put(appNo, clusterStr);
		}
		
		for (AppBean appBean : result){
			String clusterStr = map.get(appBean.getAppNo());
			if (clusterStr != null) {
				appBean.setClusterStr(clusterStr);
			}
		}
		
		sess.close();
		apps.addAll(result);
		
	}

	public static void getFuncList(String appNo, List<FuncBean> funcs) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (funcs != null)
			funcs.clear();
		else
			funcs = Lists.newArrayList();
		
		List<FuncBean> result = sess.selectList(ConfigNames.SELECT_FUNC_LIST,appNo);
		funcs.addAll(result);
		sess.close();
	}

	public static void getFuncParameterList(String funcNo, List<ParamBean> params) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (params != null)
			params.clear();
		else
			params = Lists.newArrayList();
		
		List<ParamBean> result = sess.selectList(ConfigNames.SELECT_FUNC_PARAM_LIST,funcNo);
		params.addAll(result);
		sess.close();
	}

	public static void getFuncColumnList(String funcNo, List<ResultBean> columns) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (columns != null)
			columns.clear();
		else
			columns = Lists.newArrayList();
		
		List<ResultBean> result = sess.selectList(ConfigNames.SELECT_FUNC_COLUMN_LIST,funcNo);
		columns.addAll(result);
		sess.close();
	}

	public final static int getAppNo() {

		SqlSession session = CommonValues.factory.openSession();
		int appNo = (Integer)session.selectOne(ConfigNames.SELECT_APPID);
		session.close();
		return appNo;
	}

	public static void createApp(AppBean app) {
		
		SqlSession session = CommonValues.factory.openSession(false);
		
		// insert app
		session.insert(ConfigNames.INSERT_APP, app);
		
		// insert app cluster
		RelBean rel = new RelBean().setId(app.getAppNo());
		for (Cluster cluster : app.getClusters()){
			rel.setRelId(cluster.getClusterID());
			session.insert(ConfigNames.INSERT_APP_CLUSTER, rel);
		}
		
		session.commit();
		session.close();
		
	}

	public static void deleteApp(String appNo) {
		
		SqlSession session = CommonValues.factory.openSession(false);
		
		// delete app
		session.delete(ConfigNames.DELETE_APP, appNo);
		
		// delete app cluster
		session.delete(ConfigNames.DELETE_APP_CLUSTER, appNo);
		
		// delete app func
		session.delete(ConfigNames.DELETE_APP_FUNC, appNo);

		// delete app func parameter
		session.delete(ConfigNames.DELETE_APP_FUNC_PARAMETER, appNo);

		// delete app func result
		session.delete(ConfigNames.DELETE_APP_FUNC_RESULT, appNo);
		
		session.commit();
		session.close();
		
	}

	public static void createAppFunc(FuncBean func) {
		
		SqlSession session = CommonValues.factory.openSession(false);
		func.setTableFile(StringEscapeUtils.escapeSql(func.getTableFile()));
		func.setHandlerFile(StringEscapeUtils.escapeSql(func.getHandlerFile()));
		func.setFuncNo(DefaultImpl.getAppFuncNo());
		func.setFuncStatus(CommonValues.STATUS_CREATE);
		
		// insert func
		session.insert(ConfigNames.INSERT_APP_FUNC, func);
		
		// insert func parameters
		for (int i = 0; i<func.getParams().size(); i++){
			ParamBean param = func.getParams().get(i);
			param.setFuncNo(func.getFuncNo());
			param.setParamSeq(i+1);
			session.insert(ConfigNames.INSERT_APP_FUNC_PARAM, param);
		}
		
		// insert func result table columns
		for (int i = 0; i<func.getColumns().size(); i++){
			ResultBean column = func.getColumns().get(i);
			column.setFuncNo(func.getFuncNo());
			column.setTableName(func.getTableName());
			column.setColumnSeq(i+1);
			session.insert(ConfigNames.INSERT_APP_FUNC_RESULT, column);
		}
		session.commit();
		session.close();
	}

	public static String getAppFuncNo() {
		SqlSession session = CommonValues.factory.openSession();
		int funcNo = (Integer)session.selectOne(ConfigNames.SELECT_APPFUNCID);
		session.close();
		return String.valueOf(funcNo);
	}

	public static void deleteFunc(String funcNo) {
		
		SqlSession session = CommonValues.factory.openSession(false);
		
		// delete func
		session.delete(ConfigNames.DELETE_FUNC, funcNo);

		// delete func parameter
		session.delete(ConfigNames.DELETE_FUNC_PARAMETER, funcNo);

		// delete func result
		session.delete(ConfigNames.DELETE_FUNC_RESULT, funcNo);
		
		session.commit();
		session.close();
	}

	public static void getAppCluster(String appNo, List<Cluster> clusters) {
		SqlSession sess = CommonValues.factory.openSession();
		if (clusters != null)
			clusters.clear();
		else
			clusters = Lists.newArrayList();
		
		List<Cluster> result = sess.selectList(ConfigNames.SELECT_APP_CLUSTER_LIST,appNo);
		clusters.addAll(result);
		sess.close();
	}

	public static void updateFuncStatus(FuncBean func) {
		
		SqlSession session = CommonValues.factory.openSession(false);
		
		// update func
		session.update(ConfigNames.UPDATE_APP_FUNC_STATUS, func);
		
		session.commit();
		session.close();
		
	}

	public static void updateUserStatus(UserBean user) {
		
		SqlSession session = CommonValues.factory.openSession(false);
		
		// update
		session.update(ConfigNames.UPDATE_USER_STATUS, user);
		
		session.commit();
		session.close();
	}

	public static void deleteUser(String userNo) {
		
		SqlSession session = CommonValues.factory.openSession(false);
		
		// delete user
		session.delete(ConfigNames.DELETE_USER, userNo);
		
		// delete user rights
		session.delete(ConfigNames.DELETE_USER_RIGHTS, userNo);
		
		session.commit();
		session.close();
	}

	public static void getUserList(List<UserBean> users) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (users != null)
			users.clear();
		else
			users = Lists.newArrayList();
		
		List<UserBean> result = sess.selectList(ConfigNames.SELECT_USER_LIST);
		users.addAll(result);
		sess.close();
		
	}

	public static String isExistsUser(String userNo) {
		
		SqlSession sess = CommonValues.factory.openSession();
		String flag = sess.selectOne(ConfigNames.IS_EXISTS_USER, userNo);
		sess.close();
		return flag;
		
	}

	public static void createUser(UserBean user) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		// insert user
		sess.insert(ConfigNames.INSERT_USER, user);
		
		sess.commit();
		sess.close();
	}

	public static void getAllFuncList(List<FuncBean> funcs) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (funcs != null)
			funcs.clear();
		else
			funcs = Lists.newArrayList();
		
		List<FuncBean> result = sess.selectList(ConfigNames.SELECT_ALL_FUNC_LIST);
		funcs.addAll(result);
		sess.close();
		
	}

	public static void updateUserRights(List<FuncBean> funcs, String userNo) {
		
		SqlSession sess = CommonValues.factory.openSession();
		List<String> apps = Lists.newArrayList();
		UserRightBean right = new UserRightBean().setUserNo(userNo);
		
		// delete all rights for this userNo
		sess.delete(ConfigNames.DELETE_USER_RIGHTS, userNo);
		
		for(FuncBean func:funcs){

			// insert app rights
			if (!apps.contains(func.getAppNo())){
				apps.add(func.getAppNo());
				sess.insert(ConfigNames.INSERT_USER_RIGHT, right.setObjectType(CommonValues.RIGHT_APP)
																 .setObjectNo(func.getAppNo()));
			}
			// insert func rights	
			sess.insert(ConfigNames.INSERT_USER_RIGHT, right.setObjectType(CommonValues.RIGHT_FUNC)
					 .setObjectNo(func.getFuncNo()));
		}
		sess.commit();
		sess.close();
	}

	public static void getUserFuncRights(String userNo, List<String> rights) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (rights != null)
			rights.clear();
		else
			rights = Lists.newArrayList();
		
		List<String> result = sess.selectList(ConfigNames.SELECT_USER_FUNC_RIGHTS,userNo);
		rights.addAll(result);
		sess.close();
		
	}

	public static UserBean getUserInfo(String userNo) {
		
		SqlSession sess = CommonValues.factory.openSession();
		UserBean user = sess.selectOne(ConfigNames.SELECT_USER, userNo);
		sess.close();
		return user;
	}

	public static void modifyUserPassword(UserBean user) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
	
		// modify password
		sess.update(ConfigNames.UPDATE_USER_PASSWORD, user);
		
		sess.commit();
		sess.close();
	}

	public static void updateClusterAvailable(Cluster cluster) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		// update cluster available
		sess.update(ConfigNames.UPDATE_CLUSTER_AVAILABLE, cluster);
		
		sess.commit();
		sess.close();
	}

	public static void updateClusterStatus(Cluster cluster) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		// update cluster available
		sess.update(ConfigNames.UPDATE_CLUSTER_STATUS, cluster);
		
		sess.commit();
		sess.close();
	}

	public static void deleteClusterID(String clusterID) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		// delete all info
		sess.delete(ConfigNames.DELETE_CLUSTER, clusterID);
		sess.delete(ConfigNames.DELETE_CLUSTER_NODE, clusterID);
		sess.delete(ConfigNames.DELETE_CLUSTER_CONF, clusterID);
		
		sess.commit();
		sess.close();
	}

	public static void getAppFuncParameters(String funcNo, List<ParamBean> params) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (params != null)
			params.clear();
		else
			params = Lists.newArrayList();
		
		List<ParamBean> result = sess.selectList(ConfigNames.SELECT_FUNC_PARAM_LIST,funcNo);
		params.addAll(result);
		sess.close();
	}

	public static void getFuncDataSources(String funcNo,
			List<DataSourceBean> dataSources) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (dataSources != null)
			dataSources.clear();
		else
			dataSources = Lists.newArrayList();
		
		List<DataSourceBean> result = sess.selectList(ConfigNames.SELECT_FUNC_DATASOURCES,funcNo);
		dataSources.addAll(result);
		sess.close();
	}

	public static String getDataSourceNo() {
		SqlSession sess = CommonValues.factory.openSession();
		String result = sess.selectOne(ConfigNames.SELECT_DATASOURCEID);
		sess.close();
		return result;
	}

	public static void createDataSource(DataSourceBean ds) {
		
		if (ds == null) return;

		ds.setTableFile(StringEscapeUtils.escapeSql(ds.getTableFile()));
		ds.setLoadFile(StringEscapeUtils.escapeSql(ds.getLoadFile()));
		ds.setDsNo(DefaultImpl.getDataSourceNo());
		ds.setDsStatus(CommonValues.STATUS_CREATE);
		
		SqlSession sess = CommonValues.factory.openSession(false);
			
		// insert datasource
		sess.insert(ConfigNames.INSERT_DATASOURCE,ds);
			
		sess.commit();
		sess.close();
	}

	public static void getDataSources(List<DataSourceBean> dataSources) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (dataSources != null)
			dataSources.clear();
		else
			dataSources = Lists.newArrayList();
		
		List<DataSourceBean> result = sess.selectList(ConfigNames.SELECT_DATASOURCES);
		dataSources.addAll(result);
		sess.close();
	}

	public static void updateDataSourceStatus(DataSourceBean ds) {

		SqlSession sess = CommonValues.factory.openSession(false);
		
		// update status
		sess.update(ConfigNames.UPDATE_DATASOURCE_STATUS, ds);
		
		sess.commit();
		sess.close();
	}

	public static void deleteDataSource(String ds) {

		SqlSession session = CommonValues.factory.openSession(false);
		
		// delete data source
		session.delete(ConfigNames.DELETE_DATASOURCE, ds);
		
		session.commit();
		session.close();
	}

	public static FuncBean getAppFunc(String funcNo) {
		
		SqlSession sess = CommonValues.factory.openSession();
		
		FuncBean result = sess.selectOne(ConfigNames.SELECT_FUNC,funcNo);
		sess.close();
		return result;
		
	}

	public static String getCache(CacheBean cache) {
		
		SqlSession sess = CommonValues.factory.openSession();
		
		String result = sess.selectOne(ConfigNames.SELECT_CACHE,cache);
		sess.close();
		return result;
		
	}

	public static String insertTask(TaskMessage task) {
		
		SqlSession sess = CommonValues.factory.openSession();
		
		if (task == null) return null;
		
		TaskBean taskBean = new TaskBean();
		
		// get task no 
		String taskNo = sess.selectOne(ConfigNames.SELECT_TASKID);
		taskBean.setTaskNo(taskNo);
		taskBean.setUserNo(CommonValues.getUserNo());
		taskBean.setFuncNo(task.getFuncNo());
		taskBean.setDsNo(task.getDsNo());
		taskBean.setClusterID(task.getClusterID());
		taskBean.setParams(task.getParams());
		taskBean.setTaskStatus(CommonValues.TASK_STATUS_INQUEUE);
		taskBean.setCacheID(task.getCacheID());
		taskBean.setReload(task.isReload());
		
		sess.insert(ConfigNames.INSERT_TASK,taskBean);
		sess.commit();
		sess.close();
		
		return taskNo;
		
	}

	public static void updateTaskStatus(TaskMessage task, String status) {
		
		TaskBean taskBean = new TaskBean().setTaskNo(task.getTaskNo());
		
		updateTaskStatus(taskBean,status);
		
	}

	public static void updateTaskStatus(TaskBean task, String status) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		task.setTaskStatus(status);
		
		// update status
		sess.update(ConfigNames.UPDATE_TASK_STATUS, task);
		
		sess.commit();
		sess.close();
		
	}

	public static int getUserRunningTaskNumber(String userNo) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		int result = (Integer)sess.selectOne(ConfigNames.SELECT_USER_RUNNING_TASK_NUMBER, userNo);
		
		sess.close();
		return result;
	}

	public static Cluster getClusterInfo(String clusterID) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		Cluster result = sess.selectOne(ConfigNames.SELECT_CLUSTER_INFO, clusterID);
		
		sess.close();
		return result;
	}

	public static int getClusterRunningTaskNumber(String clusterID) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		int result = (Integer)sess.selectOne(ConfigNames.SELECT_CLUSTER_RUNNING_TASK_NUMBER, clusterID);
		
		sess.close();
		return result;
	}

	public static void insertTaskLog(TaskLogBean taskLog) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.insert(ConfigNames.INSERT_TASK_LOG, taskLog);
		
		sess.close();
	}

	public static void getTasks(List<TaskListBean> tasks) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (tasks != null)
			tasks.clear();
		else
			tasks = Lists.newArrayList();
		
		List<TaskListBean> result = sess.selectList(ConfigNames.SELECT_TASKS, CommonValues.getUserNo());
		tasks.addAll(result);
		sess.close();
	}

	public static CacheBean getCacheInfo(String cacheID) {
		
		SqlSession sess = CommonValues.factory.openSession();
		
		CacheBean result = sess.selectOne(ConfigNames.SELECT_CACHE_INFO,cacheID);
		sess.close();
		return result;
	}

	public static String getCacheID() {
		
		SqlSession sess = CommonValues.factory.openSession();
		String cacheID = sess.selectOne(ConfigNames.SELECT_CACHEID);
		sess.close();
		return cacheID;
		
	}

	public static void insertTaskCache(TaskMessage task) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.insert(ConfigNames.INSERT_TASK_CACHE, task);
		
		sess.close();
	}

	public static String getTaskNewLog(String taskNo) {
		
		StringBuffer sb = new StringBuffer();
		SqlSession sess = CommonValues.factory.openSession();
		List<String> logs = sess.selectList(ConfigNames.SELECT_TASK_NEW_LOG, taskNo);
		sess.close();
		
		sb.setLength(0);
		for (String str:logs){
			sb.append(str);
		}
		
		return sb.toString();
	}

	public static void deleteDataSourceFunc(DataSourceBean bean) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.delete(ConfigNames.DELETE_DS_FUNC, bean);
		
		sess.close();
	}

	public static void getDataSourceFuncs(String dsNo, List<DataSourceBean> dataSources) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (dataSources != null)
			dataSources.clear();
		else
			dataSources = Lists.newArrayList();
		
		List<DataSourceBean> result = sess.selectList(ConfigNames.SELECT_DATASOURCE_FUNCS, dsNo);
		dataSources.addAll(result);
		sess.close();
	}

	public static void updateDataSourceFuncStatus(DataSourceBean ds) {

		SqlSession sess = CommonValues.factory.openSession(false);
		
		// update status
		sess.update(ConfigNames.UPDATE_DATASOURCE_FUNC_STATUS, ds);
		
		sess.commit();
		sess.close();
	}

	public static void createDataSourceFunc(DataSourceBean ds) {

		ds.setStatus(CommonValues.STATUS_CREATE);
		ds.setTransformFile(StringEscapeUtils.escapeSql(ds.getTransformFile()));
		SqlSession sess = CommonValues.factory.openSession(false);
		
		// insert 
		sess.insert(ConfigNames.INSERT_DATASOURCE_FUNC, ds);
		
		sess.commit();
		sess.close();
	}

	public static void getDsAvailableFuncList(String dsNo, List<FuncBean> funcs) {
		
		SqlSession sess = CommonValues.factory.openSession();
		if (funcs != null)
			funcs.clear();
		else
			funcs = Lists.newArrayList();
		
		List<FuncBean> result = sess.selectList(ConfigNames.SELECT_DS_AVAILABLE_FUNCS, dsNo);
		funcs.addAll(result);
		sess.close();
	}

	public static void deleteCacheByTask(CacheBean bean) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.delete(ConfigNames.DELETE_CACHE_BY_TASK, bean);
		
		sess.close();
	}

	public static void deleteCacheByFunc(CacheBean bean) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.delete(ConfigNames.DELETE_CACHE_BY_FUNC, bean);
		
		sess.close();
	}
	
	public static void deleteTaskLog(String taskNo) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.delete(ConfigNames.DELETE_TASK_LOG, taskNo);
		
		sess.close();
	}

	public static void deleteTaskJob(String taskNo) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.delete(ConfigNames.DELETE_TASK_JOB, taskNo);
		
		sess.close();
	}

	public static void insertTaskJob(TaskLogBean bean) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.insert(ConfigNames.INSERT_TASK_JOB, bean);
		
		sess.close();
	}

	public static int getSameTask(CacheBean cache) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		int count = (Integer)sess.selectOne(ConfigNames.SELECT_SAME_TASK, cache);
		
		sess.close();
		return count;
	}

	public static int getClusterFuncDSTaskNumber(CacheBean cache) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		int count = (Integer)sess.selectOne(ConfigNames.SELECT_CFD_TASK_NUMBER, cache);
		
		sess.close();
		return count;
	}

	public static int getClusterFuncDSReloadTaskNumber(CacheBean cache) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		int count = (Integer)sess.selectOne(ConfigNames.SELECT_CFD_RELOAD_TASK_NUMBER, cache);
		
		sess.close();
		return count;
	}

	public static void clearLog(int i) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.delete(ConfigNames.CLEAR_TASK_LOG, i);
		
		sess.close();
	}

	public static void updateCluster(Cluster cluster) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		sess.update(ConfigNames.UPDATE_CLUSTER, cluster);
		
		sess.close();
	}

	public static void getFuncInfo(FuncBean func) {
		
		SqlSession sess = CommonValues.factory.openSession(true);
		
		if (func == null) return;
		
		// get cluster info
		FuncBean info = sess.selectOne(ConfigNames.SELECT_FUNC, func.getFuncNo());
		func.setTableFile(info.getTableFile());
		func.setHandlerFile(info.getHandlerFile());
		func.setFuncName(info.getFuncName());
		func.setFuncDesc(info.getFuncDesc());
		func.setAppNo(info.getAppNo());
		func.setAppName(info.getAppName());
		func.setTableName(info.getTableName());
		
		// get cluster params
		DefaultImpl.getFuncParameterList(func.getFuncNo(), func.getParams());
		
		// get cluster columns
		DefaultImpl.getFuncColumnList(func.getFuncNo(), func.getColumns());
		
		sess.close();
		
	}

	public static void updateAppFunc(FuncBean func) {
		
		SqlSession sess = CommonValues.factory.openSession(false);
		
		if (func == null) return;
		
		func.setTableFile(StringEscapeUtils.escapeSql(func.getTableFile()));
		func.setHandlerFile(StringEscapeUtils.escapeSql(func.getHandlerFile()));
		
		// update cluster info
		sess.update(ConfigNames.UPDATE_FUNC, func);
		
		// update cluster params
			
			sess.delete(ConfigNames.DELETE_FUNC_PARAMETER,func.getFuncNo());
			
			// insert func parameters
			for (int i = 0; i<func.getParams().size(); i++){
				ParamBean param = func.getParams().get(i);
				param.setFuncNo(func.getFuncNo());
				param.setParamSeq(i+1);
				sess.insert(ConfigNames.INSERT_APP_FUNC_PARAM, param);
			}
			
		// update func columns
			
			sess.delete(ConfigNames.DELETE_FUNC_RESULT,func.getFuncNo());
			
			// insert func result table columns
			for (int i = 0; i<func.getColumns().size(); i++){
				ResultBean column = func.getColumns().get(i);
				column.setFuncNo(func.getFuncNo());
				column.setTableName(func.getTableName());
				column.setColumnSeq(i+1);
				sess.insert(ConfigNames.INSERT_APP_FUNC_RESULT, column);
			}
		
		sess.commit();
		sess.close();
		
	}	
}
